package com.example.tourstor.myself;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tourstor.BaseBean;
import com.example.tourstor.DividerItemDecoration;
import com.example.tourstor.R;

import java.util.ArrayList;
import java.util.List;

public class CollectActivity extends AppCompatActivity implements View.OnClickListener, CollectAdapter.OnItemClickListener{

    private static final int MYLIVE_MODE_CHECK = 0;
    private static final int MYLIVE_MODE_EDIT = 1;
    Toolbar collect_toolbar;

    //@BindView(R.id.recyclerview)collect_recycler
    RecyclerView mCollectRecyclerview;
    //@BindView(R.id.tv_select_num)
    TextView mTvSelectNum;
    //@BindView(R.id.btn_delete)
    Button mBtnDelete;
    //@BindView(R.id.select_all)
    TextView mSelectAll;
    //@BindView(R.id.ll_mycollection_bottom_dialog)
    LinearLayout mLlMycollectionBottomDialog;
    //@BindView(R.id.toolbar_manage)
    //TextView toolbar_manage;

    private CollectAdapter mCollectAdapter = null;
    private LinearLayoutManager mLinearLayoutManager;
    private List<BaseBean> mBaseBeans = new ArrayList<>();
    private int mEditMode = MYLIVE_MODE_CHECK;
    private boolean isSelectAll = false;
    private boolean editorStatus = false;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);

        //ButterKnife.bind(this);
        mBtnDelete = (Button) findViewById(R.id.btn_delete);
        mSelectAll = (TextView) findViewById(R.id.select_all);
        //toolbar_manage = (TextView) findViewById(R.id.toolbar_manage);
        mLlMycollectionBottomDialog = (LinearLayout) findViewById(R.id.ll_mycollection_bottom_dialog);
        mTvSelectNum = (TextView) findViewById(R.id.tv_select_num);
        setToolBar();
        initData();
        initListener();



        /*//修改ToolBar上菜单字体颜色,对单独的某一个Menu修改颜色
        getLayoutInflater().setFactory(new LayoutInflater.Factory(){

            @Override
            public View onCreateView(String name, Context context, AttributeSet attrs) {
                if (name.equalsIgnoreCase("com.apps.juuusper.tourstor.menu.menu_collect_manage")) {
                    try{
                        LayoutInflater f = LayoutInflater.from(context);
                        final View view = f.createView(name, null, attrs);
                        if(view instanceof TextView) {
                            TextView menuTv = ((TextView)view);
                            //这里你就可以修改TextView的字体颜色，大小，背景等等.
                            menuTv.setTextColor(getResources().getColor(R.color.white));
                        }
                        return view;
                    }catch (InflateException e) {
                        e.printStackTrace();
                    }catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        });*/
    }

    private void setToolBar() {
        collect_toolbar = (Toolbar) findViewById(R.id.collect_toolbar);

        collect_toolbar.setTitle("我的收藏");
        collect_toolbar.setTitleTextColor(Color.WHITE);

        this.setSupportActionBar(collect_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true);//设置返回键可用
        this.getSupportActionBar().setDisplayShowTitleEnabled(true);
    }

    private void initData() {
        mCollectRecyclerview = (RecyclerView) findViewById(R.id.collect_recycler);
        mCollectAdapter = new CollectAdapter(this, mBaseBeans);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mCollectRecyclerview.setLayoutManager(mLinearLayoutManager);
        DividerItemDecoration itemDecorationHeader = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
        itemDecorationHeader.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.divider_main_bg_height_1));
        mCollectRecyclerview.addItemDecoration(itemDecorationHeader);
        mCollectRecyclerview.setAdapter(mCollectAdapter);
        for (int i = 0; i < 30; i++) {
            BaseBean mybean = new BaseBean();

            //商家图片uri
            //mybean.setImageUri(i);
            mybean.setStoreName("这是第" + i + "个条目");

            mBaseBeans.add(mybean);
            mCollectAdapter.notifyAdapter(mBaseBeans, false);
        }
    }

    /**
     * 根据选择的数量是否为0来判断按钮的是否可点击.
     *
     * @param size
     */
    private void setBtnBackground(int size) {
        if (size != 0) {
            mBtnDelete.setBackgroundResource(R.drawable.button_noclickable_shape);
            mBtnDelete.setEnabled(true);
            mBtnDelete.setTextColor(Color.WHITE);
        } else {
            mBtnDelete.setBackgroundResource(R.drawable.button_shape);
            mBtnDelete.setEnabled(false);
            mBtnDelete.setTextColor(ContextCompat.getColor(this, R.color.color_b7b8bd));
        }
    }




    //通过LayoutInflater去创建一个view或menu时，你就可以通过实现自己的LayoutInflater.Factory来定制自己的View
    //重写Activity的onCreateOptionsMenu方法并实现相关逻辑
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_collect_manage, menu);
        return super.onCreateOptionsMenu(menu);
    }

    //监听菜单项的事件重写Activity的onOptionsItemSelected方法
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.toolbar_manage:
                //Toast.makeText(this, "收藏", Toast.LENGTH_SHORT).show();
                //setToolBarMenu();
                index = 0;
                mEditMode = mEditMode == MYLIVE_MODE_CHECK ? MYLIVE_MODE_EDIT : MYLIVE_MODE_CHECK;
                if (mEditMode == MYLIVE_MODE_EDIT) {
                    item.setTitle("取消");
                    mLlMycollectionBottomDialog.setVisibility(View.VISIBLE);
                    editorStatus = true;
                    //clearAll();
                } else {
                    item.setTitle("管理");
                    mLlMycollectionBottomDialog.setVisibility(View.GONE);
                    editorStatus = false;
                    clearAll();
                }
                mCollectAdapter.setEditMode(mEditMode);
                break;
            case android.R.id.home:
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    /*public void setToolBarMenu(){

    }*/
    private void clearAll() {
        mTvSelectNum.setText(String.valueOf(0));
        isSelectAll = false;
        mSelectAll.setText("全选");
        setBtnBackground(0);
    }


    private void initListener() {
        mCollectAdapter.setOnItemClickListener(this);
        mBtnDelete.setOnClickListener(this);
        mSelectAll.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.btn_delete:
                deleteVideo();
                break;
            case R.id.select_all:
                selectAllMain();
                break;
            default:
                break;
        }

    }

    /**
     * 全选和反选
     */
    private void selectAllMain() {
        if (mCollectAdapter == null) return;
        if (!isSelectAll) {
            for (int i = 0, j = mCollectAdapter.getMyBean().size(); i < j; i++) {
                mCollectAdapter.getMyBean().get(i).setSelect(true);
            }
            index = mCollectAdapter.getMyBean().size();
            mBtnDelete.setEnabled(true);
            mSelectAll.setText("取消全选");
            isSelectAll = true;
        } else {
            for (int i = 0, j = mCollectAdapter.getMyBean().size(); i < j; i++) {
                mCollectAdapter.getMyBean().get(i).setSelect(false);
            }
            index = 0;
            mBtnDelete.setEnabled(false);
            mSelectAll.setText("全选");
            isSelectAll = false;
        }
        mCollectAdapter.notifyDataSetChanged();
        setBtnBackground(index);
        mTvSelectNum.setText(String.valueOf(index));
    }

    /**
     * 删除逻辑
     */
    private void deleteVideo() {
        if (index == 0){
            mBtnDelete.setEnabled(false);
            return;
        }
        final AlertDialog builder = new AlertDialog.Builder(this)
                .create();
        builder.show();
        if (builder.getWindow() == null) return;
        builder.getWindow().setContentView(R.layout.pop_user);//设置弹出框加载的布局
        TextView msg = (TextView) builder.findViewById(R.id.tv_msg);
        Button cancle = (Button) builder.findViewById(R.id.btn_cancle);
        Button sure = (Button) builder.findViewById(R.id.btn_sure);
        if (msg == null || cancle == null || sure == null) return;

        if (index == 1) {
            msg.setText("删除后不可恢复，是否删除该条目？");
        } else {
            msg.setText("删除后不可恢复，是否删除这" + index + "个条目？");
        }
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = mCollectAdapter.getMyBean().size(), j =0 ; i > j; i--) {
                    BaseBean myLive = mCollectAdapter.getMyBean().get(i-1);
                    if (myLive.isSelect()) {

                        mCollectAdapter.getMyBean().remove(myLive);
                        index--;
                    }
                }
                //index = 0;
                mTvSelectNum.setText(String.valueOf(index));
                setBtnBackground(index);
                if (mCollectAdapter.getMyBean().size() == 0){
                    mLlMycollectionBottomDialog.setVisibility(View.GONE);
                }
                mCollectAdapter.notifyDataSetChanged();
                builder.dismiss();
            }
        });
    }

    @Override
    public void onItemClickListener(int pos, List<BaseBean> mBaseBeans) {

        if (editorStatus) {
            BaseBean myLive = mBaseBeans.get(pos);
            boolean isSelect = myLive.isSelect();
            if (!isSelect) {
                index++;
                myLive.setSelect(true);
                if (index == mBaseBeans.size()) {
                    isSelectAll = true;
                    mSelectAll.setText("取消全选");
                }

            } else {
                myLive.setSelect(false);
                index--;
                isSelectAll = false;
                mSelectAll.setText("全选");
            }
            setBtnBackground(index);
            mTvSelectNum.setText(String.valueOf(index));
            mCollectAdapter.notifyDataSetChanged();
        }
    }
}
